package se.thoka.twt.client.utils.validation;

/**
 * <p>
 * Asserts that the value has at least a minimum length.
 * </p>
 * 
 * @author Thobias Karlsson, Thoka
 * @since 9 jan 2010
 */
public class MinLengthValidation implements Validation {
	/**
	 * The message.
	 */
	private String message;
	/**
	 * Required minimum length
	 */
	private int minLength;

	/**
	 * Creates a new instance of {@link MinLengthValidation}.
	 * 
	 * @param length
	 *            The minimum length.
	 */
	public MinLengthValidation(int length) {
		this(length, ValidationUtil.getStrings().minLengthNotReached(
				Integer.toString(length)));
	}

	/**
	 * Creates a new instance of {@link MinLengthValidation}.
	 * 
	 * @param minLength
	 *            The minimum length.
	 * @param message
	 *            The message where <code>{0}</code> is replaced with
	 *            <code>length</code>.
	 */
	public MinLengthValidation(int minLength, String message) {
		this.message = message;
		this.minLength = minLength;

		if (message != null) {
			message = message.replaceAll("\\{0\\}", String.valueOf(minLength));
		}
	}

	@Override
	public boolean validate(String value) {
		return value != null && value.length() >= minLength;
	}

	@Override
	public String getDescription() {
		return message;
	}
}
